package cc.customer.utils;

import cc.customer.common.domain.ResponseBo;
import cc.customer.expCustomer.dao.ExEmpMapper;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**  
 * 创建时间：2018年5月25日 下午3:11:21
 * 项目名称：taskmanager  
 * @author yhsh
 * @version 1.0
 * @since JDK 1.7.0_21 
 * 类说明： 钉钉类的一些封装
 */
public class DingDingMessageUtil {
	
	private static final Logger logger= LoggerFactory.getLogger(DingDingMessageUtil.class);


	public static final String GETTOKEN="https://oapi.dingtalk.com/gettoken";


	public static final String ROUTER_RSET="https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";


	public static final String SIAPPKEY="dingu3ultvcvvcla0dln";

	public static final String SIAPPSECRET="76jnfdagx_n_e3fqgEKUvVhJWxswW12CcZxZ7h3tGTtkQCwSoTDUQg9BW6cD2LfD";

	public static final String SIAGENID="794353018";


	public static final String APPKEY="dingusfxizpitpat56lf";

	public static final String APPSECRET="8Vx9ezIyUABfcMLzQ0e5NcKp7peerhrPlyWWEQgrslOERfidRgD2Z6ogZTw7oUdZ";

	public static final String AGENID="794749871";

	@Autowired
	private static ExEmpMapper exEmpMapper;


	/**
	 * 获取钉钉的token
	 * **/
	public static String getToken(String type){
		String accdess_toiken = null;
		try {
			DingTalkClient client = new DefaultDingTalkClient(GETTOKEN);
			OapiGettokenRequest req = new OapiGettokenRequest();
			req.setHttpMethod("GET");
			if("isSi".equals(type)){
				req.setAppkey(SIAPPKEY);
				req.setAppsecret(SIAPPSECRET);
			}else{
				req.setAppkey(APPKEY);
				req.setAppsecret(APPSECRET);
			}
			OapiGettokenResponse rsp = client.execute(req);

			JSONObject obj = JSONObject.parseObject(rsp.getBody());
			if("0".equals(obj.getString("errcode"))){
				accdess_toiken = obj.getString("access_token");
			}else{
				logger.error("获取推送token错误："+obj.getString("errmsg"));
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return accdess_toiken;
	}

	/**
	 * 异步发送企业信息
	 * 、**/
	/*public static ResponseBo sendEntiseMsg(String token, String touser, String msgcontent){
		try {
			DingTalkClient client = new DefaultDingTalkClient(ROUTER_RSET);
			CorpMessageCorpconversationAsyncsendRequest req = new CorpMessageCorpconversationAsyncsendRequest();
			req.setMsgtype("oa");
			req.setAgentId(Long.valueOf(agenId));
			req.setDeptIdList("");
			if(!StringUtils.isEmpty(touser)){
				req.setUseridList(touser);
				req.setToAllUser(false);
			}else{ req.setToAllUser(true); }

			req.setMsgcontent(msgcontent);
			CorpMessageCorpconversationAsyncsendResponse rsp = client.execute(req, token);
			JSONObject object = JSONObject.parseObject(rsp.getBody());

			logger.error("推送钉钉数据返回值:"+object.toJSONString());

			if(object.containsKey("dingtalk_corp_message_corpconversation_asyncsend_response")){
				JSONObject array = object.getJSONObject("dingtalk_corp_message_corpconversation_asyncsend_response")
						.getJSONObject("result");
				return ResponseBo.ok( array);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return ResponseBo.ok("resut enpty");
	}*/


	/**
	 * 异步发送企业信息 到人
	 * 、**/
	public static ResponseBo sendNewMsg(String token, String touser, OapiMessageCorpconversationAsyncsendV2Request.OA msgcontent,String type,String exUuid){
		try {
			DingTalkClient client = new DefaultDingTalkClient(ROUTER_RSET);
			OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
			if("isSi".equals(type)){
				req.setAgentId(Long.parseLong(SIAGENID));
			}else{
				req.setAgentId(Long.parseLong(AGENID));
			}

			req.setUseridList(touser);
			req.setToAllUser(false);
			OapiMessageCorpconversationAsyncsendV2Request.Msg obj1 = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
			obj1.setMsgtype("oa");
			OapiMessageCorpconversationAsyncsendV2Request.OA obj2 = new OapiMessageCorpconversationAsyncsendV2Request.OA();
			OapiMessageCorpconversationAsyncsendV2Request.Body obj3 = new OapiMessageCorpconversationAsyncsendV2Request.Body();
			obj3.setAuthor("新里程客户系统");
			List<OapiMessageCorpconversationAsyncsendV2Request.Form> list5 = new ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form>();
			OapiMessageCorpconversationAsyncsendV2Request.Form obj6 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
			list5.add(obj6);
			obj3.setForm(list5);
			obj3.setTitle("推广资源名片");
			obj2.setBody(obj3);
			OapiMessageCorpconversationAsyncsendV2Request.Head obj7 = new OapiMessageCorpconversationAsyncsendV2Request.Head();
			obj7.setBgcolor("FFBBBBBB");
			obj2.setHead(obj7);
			obj1.setOa(msgcontent);
			req.setMsg(obj1);
			OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, token);
			if(rsp.getErrcode()==0){
				logger.info("钉钉推送信息成功，信息task_id："+rsp.getTaskId()+",成功接收信息id:"+touser);
			}
			//exEmpMapper.updateTaskId(exUuid,rsp.getTaskId()+"");
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return ResponseBo.ok();
	}
	
}
